﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using ExtBusiness;
using ExtBusiness.User;
using ExtBusiness.Interface;
using ExtBusiness.CommonBusiness;
using ChBusiness.CommonBusiness;

public partial class WebPubManager_UserList :PageBase
{
    private UserManager cUser = new UserManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        string strId = Request.Params["id"];
        InterfaceOperation infaceOperation = new InterfaceOperation();
        string strXml = string.Empty;

        try
        {
            if (Request.Params["action"] == "delete")
            {
                //更新用户信息 
                //strXml = infaceOperation.GetUserInfoXml(strId, "delete");
                //if (!string.IsNullOrEmpty(strXml))
                //{
                //    DxInterface.IdsService idsTemp = new DxInterface.IdsService();

                //    string strCheckCode = Common.Encode.Md5(DateTime.Now.ToShortDateString());
                //    string strReturn = idsTemp.UserInfo(strXml, strCheckCode);

                //    if (!string.IsNullOrEmpty(strReturn))
                //    {
                //        if (XmlHelper.HaveTargetString(strReturn, "UserInfoResponse", "true"))
                //        {
                //            UserManager.DeleteUser(strId);//删除用户

                //        }
                //        else
                //        {
                //            infaceOperation.UpdateExceptionAndXml(strXml, "接口更新失败",10);
                //        }
                //    }
                //    else 
                //    {
                //        infaceOperation.UpdateExceptionAndXml(strXml, "接口返回值为空", 10);
                //    }
                //}
                //else
                //{
                    UserManager.DeleteUser(strId);//删除用户
                    //添加操作日志 addby zhongjian 20100421
                    //获取操作者ip地址
                    string myIP = Common.IPOperation.GetIpAddress();

                    string strRemark = "删除用户信息。用户ID: " + strId + ",可以此ID,在用户历史表(sys_user_old)中查询相关信息. ";
                    SystemLogs.AddSystemLogs(Session["UserName"].ToString(), "delete", strRemark, "",myIP);
                //}
            }
            if (Request.Params["action"] == "reset")
            {
                //先更新密码
                UserManager.UpdatePassword(strId);

                //更新用户信息 
                //strXml = infaceOperation.GetUserInfoXml(strId, "update");

                //if (!string.IsNullOrEmpty(strXml))
                //{
                //    DxInterface.IdsService idsTemp = new DxInterface.IdsService();

                //    string strCheckCode = Common.Encode.Md5(DateTime.Now.ToShortDateString());
                //    string strReturn = idsTemp.UserInfo(strXml, strCheckCode);

                //    if (!string.IsNullOrEmpty(strReturn))
                //    {
                //        if (!XmlHelper.HaveTargetString(strReturn, "UserInfoResponse", "true"))
                //        {
                //            infaceOperation.UpdateExceptionAndXml(strXml, "接口更新失败", 10);
                //        }
                //    }
                //    else
                //    {
                //        infaceOperation.UpdateExceptionAndXml(strXml, "接口返回值为空", 10);
                //    }
                //}
            }
        }
        catch (Exception ex)
        {
            if (!string.IsNullOrEmpty(strXml))
            {
                infaceOperation.UpdateExceptionAndXml(strXml, ex.Message, 10);
            }
        }
        GridviewDatabind();
        
    }

    #region 数据绑定
    /// <summary>
    /// Gridviews the databind.
    /// </summary>
    private void GridviewDatabind()
    {
        string strUnitsID = Request.Params["unitsid"];
        string strUnitsName = Request.Params["unitsname"];
        if (!string.IsNullOrEmpty(strUnitsName))
        {
            txtUnitsName.Text = strUnitsName;
        }
        if (!string.IsNullOrEmpty(strUnitsID))
        {
            btnReturn.Visible = true;
        }
        else
        {
            btnReturn.Visible = false;
        }
        lblAdd.Text = string.Format("<a href='UserEdit.aspx?unitsid={0}&unitsname={1}'>添加用户</a>", strUnitsID, strUnitsName);

        DataTable dt = cUser.GetUnitUser(txtUserID.Text.Trim(), txtUserName.Text.Trim(), txtUnitsName.Text.Trim(), strUnitsID, ddlGly.SelectedValue);
        gridviewCar.DataSource = dt;
        gridviewCar.RecordCount = dt.Rows.Count;
        gridviewCar.DataBind();
    }
    #endregion 

    /// <summary>
    /// datagrid数据加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void recvgrid_OnLoadData(object sender, EventArgs e)
    {
        this.GridviewDatabind();
    }

    #region 行创建
    /// <summary>
    /// 行创建
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gridviewCar_RowCreated(object sender, GridViewRowEventArgs e)
    {
        switch (e.Row.RowType)
        {
            case DataControlRowType.DataRow:

                DataRowView drv = e.Row.DataItem as DataRowView;
                if (drv == null)
                {
                    break;
                }
                HyperLink hyedit = e.Row.FindControl("hyedit") as HyperLink;
                if (hyedit != null)
                {
                    hyedit.NavigateUrl = "UserEdit.aspx?id=" + drv["id"] + "&unitsid=" + drv["unitsid"] + "&unitsname=" + drv["unitsname"];
                }

                HyperLink hydel = e.Row.FindControl("hydel") as HyperLink;
                if (hydel != null)
                {
                    hydel.Attributes.Add("OnClick", "return confirm('确定要删除该用户?')");
                    hydel.NavigateUrl = "UserList.aspx?action=delete&id=" + drv["id"];
                }

                LinkButton lbtnUnitsAdmin = e.Row.FindControl("lbtnUnitsAdmin") as LinkButton;
                if (lbtnUnitsAdmin != null)
                {
                    if (!Convert.IsDBNull(drv["usertype"]))
                    {
                        lbtnUnitsAdmin.Visible = drv["usertype"].ToString() == "1" ? true : false;
                    }

                    if (!Convert.IsDBNull(drv["unitsadmin"]))
                    {
                        int nAdmin = Convert.ToInt32(drv["unitsadmin"]);
                        lbtnUnitsAdmin.Text = nAdmin == 1 ? "取消管理员" : "设为管理员";

                        if (nAdmin == 1)
                        {
                            e.Row.BackColor = Color.Silver;
                        }
                    }
                    lbtnUnitsAdmin.ToolTip = drv["unitsadmin"].ToString();
                    lbtnUnitsAdmin.CommandArgument = drv["ID"].ToString();
                }

                break;
        }
    }
    #endregion

    /// <summary>
    /// 设置或取消单位管理员
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbtnUnitsAdmin_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;
        string strID = lbtn.CommandArgument;
        string strUnitsAdmin = lbtn.ToolTip;
        if (strUnitsAdmin == "1")
        {
            UserManager.SaveUserType(strID, "0");
            lbtn.Text = "设为管理员";
        }
        else
        {
            UserManager.SaveUserType(strID, "1");
            lbtn.Text = "取消管理员";
        }

    }
    protected void btnRefer_Click(object sender, EventArgs e)
    {
        GridviewDatabind();
    }

    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("UnitsList.aspx");
    }
}
